<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="输入搜素" prop="tableName">
        <el-input v-model="queryParams.tableName" placeholder="成员账号/姓名" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="所属部门" prop="tableComment">
        <el-input v-model="queryParams.tableComment" placeholder="全部" clearable style="width: 200px" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
      </el-form-item>
      <el-form-item>
        <right-toolbar :search="false" v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-form-item>
    </el-form>
    <el-row justify="space-between" class="headline">
      <el-col :span="2">
        <span>数据列表</span>
      </el-col>
      <el-col :span="2">
        <el-button @click="staffAdd">添加</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="tableList">
      <!-- <el-table-column type="selection" align="center" width="55"></el-table-column> -->
      <el-table-column label="序号" type="index" width="50" align="center">
        <template #default="scope">
          <span>{{ (queryParams.page_count - 1) * queryParams.page_size + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成员账号" align="center" prop="tableName" :show-overflow-tooltip="true" />
      <el-table-column label="姓名" align="center" prop="tableComment" :show-overflow-tooltip="true" />
      <el-table-column label="手机号" align="center" prop="className" :show-overflow-tooltip="true" />
      <el-table-column label="邮箱地址" align="center" prop="createTime" width="160" />
      <el-table-column label="所属部门" align="center" prop="updateTime" />
      <el-table-column label="成员角色" align="center" prop="updateTime" />
      <el-table-column label="最后登录" align="center" prop="updateTime" width="160" />
      <el-table-column label="是否启用" align="center" prop="updateTime"
        ><template #default="scope"> <el-switch v-model="value1" /> </template
      ></el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <!-- <el-tooltip content="预览" placement="top">
            <el-button link type="primary" icon="View" @click="handlePreview(scope.row)" v-hasPermi="['tool:gen:preview']"></el-button>
          </el-tooltip> -->
          <el-tooltip content="编辑" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleEditTable(scope.row)" v-hasPermi="['tool:gen:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tool:gen:remove']"></el-button>
          </el-tooltip>
          <!-- <el-tooltip content="同步" placement="top">
            <el-button link type="primary" icon="Refresh" @click="handleSynchDb(scope.row)" v-hasPermi="['tool:gen:edit']"></el-button>
          </el-tooltip>
          <el-tooltip content="生成代码" placement="top">
            <el-button link type="primary" icon="Download" @click="handleGenTable(scope.row)" v-hasPermi="['tool:gen:code']"></el-button>
          </el-tooltip> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page_count" v-model:limit="queryParams.page_size" @pagination="getList" />
    <!-- 预览界面 -->
    <el-dialog :title="preview.title" v-model="preview.open" width="80%" top="5vh" append-to-body class="scrollbar">
      <el-tabs v-model="preview.activeName">
        <el-tab-pane
          v-for="(value, key) in preview.data"
          :label="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
          :name="key.substring(key.lastIndexOf('/') + 1, key.indexOf('.vm'))"
          :key="value"
        >
          <el-link :underline="false" icon="DocumentCopy" v-copyText="value" v-copyText:callback="copyTextSuccess" style="float: right">&nbsp;复制</el-link>
          <pre>{{ value }}</pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script setup name="Gen">
import { listTable, previewTable, delTable, genCode, synchDb } from "@/api/tool/gen";
import router from "@/router";

const route = useRoute();
const { proxy } = getCurrentInstance();

const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const tableNames = ref([]);
const dateRange = ref([]);
const uniqueId = ref("");
const value1 = ref(true);

const data = reactive({
  queryParams: {
    page_count: 1,
    page_size: 10,
    tableName: undefined,
    tableComment: undefined,
  },
  preview: {
    open: false,
    title: "代码预览",
    data: {},
    activeName: "domain.java",
  },
});

const { queryParams, preview } = toRefs(data);

onActivated(() => {
  const time = route.query.t;
  if (time != null && time != uniqueId.value) {
    uniqueId.value = time;
    queryParams.value.page_count = Number(route.query.page_count);
    dateRange.value = [];
    proxy.resetForm("queryForm");
    getList();
  }
});

function staffAdd() {
  router.push("member");
}

/** 查询表集合 */
function getList() {
  loading.value = true;
  listTable(proxy.addDateRange(queryParams.value, dateRange.value)).then((response) => {
    tableList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.page_count = 1;
  getList();
}

/** 修改按钮操作 */
function handleEditTable(row) {
  const tableId = row.tableId || ids.value[0];
  router.push({ path: "member", query: { page_count: queryParams.value.page_count } });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const tableIds = row.tableId || ids.value;
  proxy.$modal
    .confirm('是否确认删除表编号为"' + tableIds + '"的数据项？')
    .then(function () {
      return delTable(tableIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
<style lang="scss" scoped>
.headline {
  background-color: #f3f3f3;
  text-align: center;
  span {
    display: block;
    padding-top: 5px;
  }
}
</style>
